package com.pubinfo.passbook.engine.process.variable.medicine.payinformation;

import com.pubinfo.passbook.common.entity.TEnginePlaceholderConst;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.engine.EngineComputePlaceHolderConstService;
import com.pubinfo.passbook.common.service.ruler.InformationService;
import com.pubinfo.passbook.engine.utils.ApplicationContextUilts;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.List;

/**
 * 诊查费 (省公司)
 * <p>
 * * 默认情况, 该参数处理器会优先读取常量占位符表中名**包含**:
 * * ["省公司本部诊查费上限"] 名称的常量占位符来返回不同值.
 * * 如果不存在该些常量, 则默认使用该类中默认的值. 默认上限：10
 *
 * @author zhouch
 * @date 2022/3/14 9:17
 */
@Slf4j
public class SGSByCostNameProcessor extends PayinformationVariableProcessor {

    // 温州市泰顺最大一档退休报销金额
    private static final BigDecimal COST_MAX = new BigDecimal(10);

    private static final String SHENG_GONG_SI_COST_MAX_KEYWORD = "省公司本部诊查费上限";

    EngineComputePlaceHolderConstService constService;

    InformationService informationService;

    public SGSByCostNameProcessor() {
        informationService = ApplicationContextUilts.getBean(InformationService.class);
        constService = ApplicationContextUilts.getBean(EngineComputePlaceHolderConstService.class);
    }

    @Override
    public BigDecimal get(PayInformation data) {

        BigDecimal costMax = COST_MAX;

        BigDecimal indagation = informationService.getAmountByCostName(data.getEInvoiceId());

        if (indagation.equals(new BigDecimal(-0.0000999))) {
            cache.markNeedAudit();
            log.info("{} ==> 需要人工审核.", getClass().getName());
            indagation = BigDecimal.ZERO;
        }

        List<TEnginePlaceholderConst> constants = constService.likeName(SHENG_GONG_SI_COST_MAX_KEYWORD);
        if (!constants.isEmpty()) {
            if (constants.size() > 1) {
                log.warn("[获取诊查费 (省公司) 处理器]: 查询到有多个'{}'常量占位符, 注意检查计算是否错误."
                        , SHENG_GONG_SI_COST_MAX_KEYWORD);
            }
            // 有多个的话, 默认取查询到的第一个
            costMax = constants.get(0).getValue();
        }

        return indagation.intValue() > costMax.intValue() ? costMax : indagation;
    }

}
